package com.springboot.mall.controller;

import com.springboot.mall.common.ApiRestResponse;
import com.springboot.mall.common.Constant;
import com.springboot.mall.exception.MallException;
import com.springboot.mall.exception.MallExceptionEnum;
import com.springboot.mall.model.entity.User;
import com.springboot.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/*
 *
 *用户控制器
 *@data 2022-05-07
 */
@Controller
public class UserController {

    @Autowired
    private  UserService userService;

    @ApiOperation("测试接口")
    @GetMapping("/test")
    @ResponseBody
    public User personalPage(){
        User user = userService.getUser();
        return user;
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("userName")String userName,
                                    @RequestParam("password")String password) throws MallException {
        //注册校验
        if(StringUtils.isEmpty(userName)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if(StringUtils.isEmpty(password)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度的校验，长度不能小于8
        if(password.length() < 8){
            return ApiRestResponse.error(MallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        userService.register(userName,password);
        return ApiRestResponse.success();

    }

    @ApiOperation("登录")
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("userName")String userName,
                                 @RequestParam("password")String password,
                                 HttpSession session) throws MallException {

        //登录校验
        if(StringUtils.isEmpty(userName)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if(StringUtils.isEmpty(password)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(userName, password);
        //不能将密码返回
        user.setPassword(null);
        session.setAttribute(Constant.USER,user);
        return ApiRestResponse.success(user);
    }

    @ApiOperation("签名更新")
    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse updateUserInfo(HttpSession session,
                                          @RequestParam("signature") String signature)
            throws MallException {
        User currentUser = (User) session.getAttribute(Constant.USER);
        if (currentUser == null){
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateInfo(user);
        return ApiRestResponse.success();

    }

    @ApiOperation("登出")
    @PostMapping("/user/logout")
    @ResponseBody
    public ApiRestResponse logOut(HttpSession session){
        session.removeAttribute(Constant.USER);
        return ApiRestResponse.success();
    }

    @ApiOperation("管理员登录")
    @PostMapping("/adminLogin")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("userName")String userName,
                                      @RequestParam("password")String password,
                                      HttpSession session) throws MallException {
        //登录校验
        if(StringUtils.isEmpty(userName)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_USER_NAME);
        }
        if(StringUtils.isEmpty(password)){
            return ApiRestResponse.error(MallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(userName, password);
        //校验是否是管理员
        if (userService.checkAdminRole(user)) {
            //不能将密码返回
            user.setPassword(null);
            session.setAttribute(Constant.USER,user);
            return ApiRestResponse.success(user);
        }else{
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN);
        }

    }




}
